﻿using BW.Mes.WinForms.BasicData;
using BW.Mes.WinForms.Login;
using BW.Mes.WinForms.Process;
using BW.Mes.WinForms.Process.Aduit;
using BW.Mes.WinForms.Process.AuditProcess;
using BW.Mes.WinForms.Process.OperationModel;
using BW.Mes.WinForms.Production.Order;
using BW.Mes.WinForms.Production.ProductionDate;
using BW.Mes.WinForms.Production.ProductionState;
using BW.Mes.WinForms.WareHouse;
using BW.Mes.WinForms.WareHouse.ExternalLabel;
using BW.Mes.WinForms.WareHouse.Libraryrecord;
using BW.Mes.WinForms.WareHouse.SuppliedMaterials;
using BW.Mes.WinForms.Workbench;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Web.UI.HtmlControls;
using System.Windows.Forms;

namespace BW.Mes.WinForms
{
    public partial class Form1 : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        private readonly HttpClient _httpClient;
        private readonly string _wareHouseApiBaseUrl = "http://120.26.197.100:8022/";


        public Form1()
        {
            InitializeComponent();
            this.IsMdiContainer = true;
            //初始化httpClient
            _httpClient=new HttpClient();
            _httpClient.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
        }

        
        /// <summary>
        /// 仓库管理-》仓库定义
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void warehouse_Cilcked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool warehouses = Circulation.ActivateExistingMdiChild(this, e.Link.Caption);
            if (!warehouses)
            {
               WareHoustForm warehouse = new WareHoustForm();
                warehouse.MdiParent = this;
                warehouse.Show();
            }
        }
        /// <summary>
        /// 仓库管理-》货架/库区
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Storage_Clicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool storage = Circulation.ActivateExistingMdiChild(this, e.Link.Caption);
            if (!storage)
            {
              StorageForm storageForm = new StorageForm();
                storageForm.MdiParent = this;
                storageForm.Show();
            }
        }

        private void Click_Process(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            foreach (Form childForm in this.MdiChildren)
            {
                if (childForm is ProcessDataForm)
                {
                    childForm.Activate();
                    return;
                }
            }

            ProcessDataForm basicForm = new ProcessDataForm();
            basicForm.MdiParent = this;
            basicForm.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // 设置窗体为全屏显示
            this.WindowState = FormWindowState.Maximized;
            
            // 设置所有菜单组为展开状态
            navBarGroup1.Expanded = true;
            navBarGroup2.Expanded = true;
            AuditGroup3.Expanded = true;
            Produce.Expanded = true;
            navBarGroup4.Expanded = true;
            DashBoard.Expanded = true;

            foreach (Form childForm in this.MdiChildren)
            {
                if (childForm is GetworkbenchForm)
                {
                    childForm.Activate();
                    return;
                }
            }

            // 弹出嵌入 Vue 的仪表台页面
            string vueUrl = "http://120.26.197.100:7093/workbench";
            var dialog = new GetworkbenchForm(vueUrl);
            dialog.MdiParent = this;
            dialog.Show();
        }

       






        //工单管理的点击事件
        private void Order_Clicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool storage = Circulation.ActivateExistingMdiChild(this, e.Link.Caption);
            if (!storage)
            {
                OrderForm order = new OrderForm();
                order.MdiParent = this;
                order.Show();
            }
        }
      


        private void Workshop_Chlicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool workshops = Circulation.ActivateExistingMdiChild(this, e.Link.Caption);
            if (!workshops)
            {
                WorkshopForm workshop = new WorkshopForm();
                workshop.MdiParent = this;
                workshop.Show();
            }
        }

        private void Line_Chlicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool lines = Circulation.ActivateExistingMdiChild(this, e.Link.Caption);
            if (!lines)
            {
                LineForm1 line = new LineForm1();
                line.MdiParent = this;
                line.Show();
            }
        }

        private void Material_Chlicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool materials = Circulation.ActivateExistingMdiChild(this, e.Link.Caption);
            if (!materials)
            {
                MaterialForm material = new MaterialForm();
                material.MdiParent = this;
                material.Show();
            }
        }

        private void Product_Chlicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool product = Circulation.ActivateExistingMdiChild(this, e.Link.Caption);
            if (!product)
            {
                ProductForm products = new ProductForm();
                products.MdiParent = this;
                products.Show();
            }
        }

        private void Bom_Chlicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool boms = Circulation.ActivateExistingMdiChild(this, e.Link.Caption);
            if (!boms)
            {
                BomForm bom = new BomForm();
                bom.MdiParent = this;
                bom.Show();
            }
        }

        private void Supperial_Chlicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool supperial = Circulation.ActivateExistingMdiChild(this, e.Link.Caption);
            if (!supperial)
            {
                SupplierForm supperials = new SupplierForm();
                supperials.MdiParent = this;
                supperials.Show();
            }
        }

        private void kugl_Chlicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool customer = Circulation.ActivateExistingMdiChild(this, e.Link.Caption);
            if (!customer)
            {
                CustomerForm customers = new CustomerForm();
                customers.MdiParent = this;
                customers.Show();
            }
        }
        /// <summary>
        /// 排版管理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void productiondate_Click(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            foreach (Form childForm in this.MdiChildren)
            {
                if (childForm is ProductionDateForm)
                {
                    childForm.Activate();
                    return;
                }
            }

            ProductionDateForm date = new ProductionDateForm();
            date.MdiParent = this;
            date.Show();
        }
        /// <summary>
        /// 库位设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void StorageLocation_Clicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool storage = Circulation.ActivateExistingMdiChild(this, e.Link.Caption);
            if (!storage)
            {
                StorageLocationForm storageLocationForm = new StorageLocationForm();
                storageLocationForm.MdiParent = this;
                storageLocationForm.Show();
            }
        }

        

        private void OperationDataForm_Clicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {

            foreach (Form childForm in this.MdiChildren)
            {
                if (childForm is OperationDataForm)
                {
                    childForm.Activate();
                    return;
                }
            }

            OperationDataForm basicForm = new OperationDataForm();
            basicForm.MdiParent = this;
            basicForm.Show();
          
        }


        private void Externallabel_Clicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool storage = Circulation.ActivateExistingMdiChild(this, e.Link.Caption);
            if (!storage)
            {
                ExternalLabelForm externalLabelForm = new ExternalLabelForm();
                externalLabelForm.MdiParent = this;
                externalLabelForm.Show();
            }
        }

        private void SuppliedMaterials_Clicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            foreach (Form childForm in this.MdiChildren)
            {
                if (childForm is SuppliedMaterialsForm)
                {
                    childForm.Activate();
                    return;
                }
            }
            SuppliedMaterialsForm suppliedMaterials = new SuppliedMaterialsForm();
                suppliedMaterials.MdiParent = this;
                suppliedMaterials.Show();
            
        }

        /// <summary>
        /// 产品锁定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProductionTrue_Click(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool storage = Circulation.ActivateExistingMdiChild(this, e.Link.Caption);
            if (!storage)
            {
                ProduceTrueForm producetrue = new ProduceTrueForm();
                producetrue.MdiParent = this;
                producetrue.Show();
            }
        }
        /// <summary>
        /// 产品解锁
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ProductionFalse_Click(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool storage = Circulation.ActivateExistingMdiChild(this, e.Link.Caption);
            if (!storage)
            {
                ProduceFalseForm producefalse = new ProduceFalseForm();
                producefalse.MdiParent = this;
                producefalse.Show();
            }
        }

        private void suppli_Clicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            bool storage = Circulation.ActivateExistingMdiChild(this, e.Link.Caption);
            if (!storage)
            {
                GetSuppliedMaterialsForm getSuppliedMaterialsForm = new GetSuppliedMaterialsForm();
                getSuppliedMaterialsForm.MdiParent = this;
                getSuppliedMaterialsForm.Show();
            }
        }

        private void Libraryrecord_Clicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            foreach (Form childForm in this.MdiChildren)
            {
                if (childForm is GetLibraryrecordForm)
                {
                    childForm.Activate();
                    return;
                }
            }
            GetLibraryrecordForm getLibraryrecordForm = new GetLibraryrecordForm();
                getLibraryrecordForm.MdiParent = this;
                getLibraryrecordForm.Show();
            
        }
        /// <summary>
        /// 获取出入库的流水
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void serialNumber_Click(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            foreach (Form childForm in this.MdiChildren)
            {
                if (childForm is SerialNumberForm)
                {
                    childForm.Activate();
                    return;
                }
            }
            SerialNumberForm serialNumberForm = new SerialNumberForm();
            serialNumberForm.MdiParent = this;
            serialNumberForm.Show();
        }

        private void workbench_Click(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            foreach (Form childForm in this.MdiChildren)
            {
                if (childForm is GetworkbenchForm)
                {
                    childForm.Activate();
                    return;
                }
            }

            // 弹出嵌入 Vue 的仪表台页面
            string vueUrl = "http://localhost:3000/workbench";
            var dialog = new GetworkbenchForm(vueUrl);
            dialog.MdiParent = this;
            dialog.Show();
        }

        private void navBarControl1_Click(object sender, EventArgs e)
        {

        }

       

        private void auditprocess_click(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            foreach (Form childForm in this.MdiChildren)
            {
                if (childForm is AuditProcessForm)
                {
                    childForm.Activate();
                    return;
                }
            }

            AuditProcessForm basicForm = new AuditProcessForm();
            basicForm.MdiParent = this;
            basicForm.Show();
        }

        private void audit_click(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            foreach (Form childForm in this.MdiChildren)
            {
                if (childForm is WaitAduitForm)
                {
                    childForm.Activate();
                    return;
                }
            }

            WaitAduitForm basicForm = new WaitAduitForm();
            basicForm.MdiParent = this;
            basicForm.Show();
        }
        
        // 添加Ribbon按钮点击事件处理程序
        
        /// <summary>
        /// 基础数据按钮点击事件
        /// </summary>
        private void btnBasicData_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            // 确保基础数据菜单展开
            navBarGroup1.Expanded = true;
        }
        
        /// <summary>
        /// 工序工单按钮点击事件
        /// </summary>
        private void btnProcess_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            // 确保工序工单菜单展开
            navBarGroup2.Expanded = true;
        }
        
        /// <summary>
        /// 审批工单按钮点击事件
        /// </summary>
        private void btnAudit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            // 确保审批工单菜单展开
            AuditGroup3.Expanded = true;
        }
        
        /// <summary>
        /// 生产管理按钮点击事件
        /// </summary>
        private void btnProduce_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            // 确保生产管理菜单展开
            Produce.Expanded = true;
        }
        
        /// <summary>
        /// 仓库管理按钮点击事件
        /// </summary>
        private void btnWareHouse_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            // 确保仓库管理菜单展开
            navBarGroup4.Expanded = true;
        }
        
        /// <summary>
        /// 仪表盘按钮点击事件
        /// </summary>
        private void btnDashBoard_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            // 确保仪表盘菜单展开并触发工作台点击
            DashBoard.Expanded = true;
            
            // 打开工作台
            foreach (Form childForm in this.MdiChildren)
            {
                if (childForm is GetworkbenchForm)
                {
                    childForm.Activate();
                    return;
                }
            }

            string vueUrl = "http://localhost:3000/workbench";
            var dialog = new GetworkbenchForm(vueUrl);
            dialog.MdiParent = this;
            dialog.Show();
        }
        
        /// <summary>
        /// 登录窗口按钮点击事件
        /// </summary>
        private void btnLogin_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            // 显示登录窗口
            LoginForm loginForm = new LoginForm();
            loginForm.Show();
            this.Hide();
        }
    }
}
